﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class YARNSTOCKData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";

        public int ThreadCount;
        private string OrderNum;
        DataTable tbWGSummary;
        public YARNSTOCKData()
            : base()
        {
            
            tbWGSummary = new DataTable("WGGSUM");
            tbWGSummary.Columns.Add("ARTICLE", typeof(string));
            tbWGSummary.Columns.Add("WBBDPART", typeof(string));
            tbWGSummary.Columns.Add("FACTORY", typeof(string));
            tbWGSummary.Columns.Add("WBEWMTR", typeof(double));
            tbWGSummary.Columns.Add("PCNUM", typeof(int));
        }
        public YARNSTOCKData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string LOT,BATCH,ART,VAR,FARBE,BEZ;
            LOT = dwABPOS["LOT"].ToString();
            BATCH = dwABPOS["BATCH"].ToString();
            ART = dwABPOS["ART"].ToString();
            VAR = dwABPOS["VAR"].ToString();
            FARBE = dwABPOS["FARBE"].ToString();
            BEZ = dwABPOS["BEZ"].ToString();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                
                or1.CommandText = "SELECT GPEINDAT,CODE,GLLB,GLLP,ART,VAR,DES,BEZ,FARBE,COLOR,LIEFNR, "
                    + " SUPPLIER,BATCH,SUPPLIERBATCH,LOT,SUM(WEI) AS WEI,SUM(NVL(ALLWEI,0)) AS ALLWEI,REMARK  "
                    + " FROM ( SELECT B.GPEINDAT,TRIM(C.GSAGARNNR) AS CODE,A.GLLB ,A.GLLP, "
                    + " B.GPGART AS ART,B.GPGVAR AS VAR, "
                    + " TRIM(C.GSHANDLNR) AS DES,TRIM(C.GSGARNBEZ) AS BEZ, "
                    + " B.GPGFARBE AS FARBE,TRIM(D.GSCBEZ) AS COLOR, "
                    + " B.GPLIEFNR AS LIEFNR,TRIM(E.LSNAME1) AS SUPPLIER, "
                    + " B.GPPARTIE AS BATCH,B.GPSPPART AS SUPPLIERBATCH,B.GPFBPART AS LOT, "
                    + " A.GLLBEHI,A.GLBSTNET/1000 AS WEI, "
                    + " (SELECT SUM(T.ZUEINTKG-T.ZUENTNKG)*0.001 FROM ZUTEI T  "
                    + " WHERE T.ZUPARTIE = A.GLPARTIE AND T.ZUPARTIEKZ = A.GLPARTIEKZ  "
                    + " AND T.ZULBEHI = A.GLLBEHI AND T.ZUERLKZ<>'J') AS ALLWEI,TRIM(B.GPBEM1)||' '||TRIM(B.GPBEM2) AS REMARK "
                    + "   FROM GLLG A, GPDAT B, GSDAT C, GSCOLOR D, LIEFDAT E "
                    + "  WHERE A.GLMANDANT = B.GPMANDANT AND A.GLPARTIE = B.GPPARTIE "
                    + "    AND A.GLPARTIEKZ = B.GPPARTIEKZ AND B.GPMANDANT = C.GSMANDANT "
                    + "   AND B.GPGART = C.GSART AND B.GPGVAR = C.GSVAR "
                    + "    AND B.GPMANDANT = D.GSCMANDANT(+) AND B.GPGART = D.GSCART(+) "
                    + "    AND B.GPGVAR = D.GSCVAR(+) AND B.GPGFARBE = D.GSCFARBE(+) "
                    + "    AND B.GPMANDANT = E.LSMANDANT(+) AND B.GPLIEFNR = E.LSLIEFNR(+) "
                    + "    AND B.GPSPPART LIKE '%" + LOT + "%' AND (B.GPPARTIE = '" + BATCH + "' OR '" + BATCH + "'='0000000') "
                    + " AND C.GSGARNBEZ LIKE '%" + BEZ + "%' AND B.GPGART LIKE '%" + ART + "%' "
                    + " AND B.GPGVAR LIKE '%" + VAR + "%' AND B.GPGFARBE LIKE '%" + FARBE + "%' "
                    + "    AND A.GLLO = 'HL' AND (A.GLLB LIKE '1%' OR A.GLLB IN ('JS','EX')) "
                    + " )T GROUP BY GPEINDAT,CODE,GLLB,GLLP,ART,VAR,DES,BEZ,FARBE,COLOR,LIEFNR, "
                    + " SUPPLIER,BATCH,SUPPLIERBATCH,LOT,REMARK ORDER BY 5,3,4 "
                    ; 
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "YARNSTOCK";
                    SetDefault(tableLocal);
                    byte[] b;
                    string value; 
                    string sTemp = ""; 
                    string Column = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "DES";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "BEZ";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "COLOR";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "SUPPLIER";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "REMARK";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                    } 
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        
        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}